package com.example.springboot159.config;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration
public class ESConfig {

    private static TransportClient client ;


    @SuppressWarnings("resource")
    public static TransportClient getTransPortClient() throws UnknownHostException {

        if(client == null) {
            Settings settings = Settings.builder()
                    .put("cluster.name", "campActivityNew512")
                    .put("client.transport.sniff", true).build();

            //自动把集群下的机器添加到列表中new PreBuiltTransportClient(settings,c).addTransportAddress((TransportAddress) new InetSocketAddress(InetAddress.getByName("192.168.26.160"), 19300))
            //.addTransportAddress((TransportAddress) new InetSocketAddress(InetAddress.getByName("192.168.26.210"), 19300));

            client = new PreBuiltTransportClient(settings)
                  //  .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.26.210"),19300))
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.174.136"),9200));
        }
        return client;
    }

    public static void close() throws UnknownHostException {
        if (client != null) {
            client.close();
        }
    }
}